package com.jie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.pojo.Blog;
import com.jie.pojo.Type;
import com.jie.service.BlogService;
import com.jie.service.TypeService;
import com.jie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/17/19:52
 * @Description:
 */
@Controller
@Slf4j
@RequestMapping("/")
public class TypeShowController {
    @Autowired
    private TypeService typeService;

    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    @GetMapping("/types/{id}")
    public String types(@RequestParam(defaultValue = "1",value = "page") int page,
                        @RequestParam(defaultValue = "8",value = "pageSize") Integer pageSize,
                        Model model, @PathVariable Long id){
        List<Type> list = typeService.list();
        if (id==-1){
            id=list.get(0).getId();
        }


        Page<Blog> pageInfo =new Page<>(page,pageSize);
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Blog::getType,id);
        queryWrapper.orderByDesc(Blog::getUpdateTime);
        //b-list
        for (Type type : list) {
            LambdaQueryWrapper<Blog> queryWrappers=new LambdaQueryWrapper<>();
            queryWrappers.eq(Blog::getType,type.getId());
            List<Blog> list1 = blogService.list(queryWrappers);
            type.setBlogList(list1);
        }

        Page<Blog> page1 = blogService.page(pageInfo, queryWrapper);
        for (Blog record : page1.getRecords()) {
            record.setUser(userService.list().get(0));
            record.setTypes(typeService.getById(record.getType()));
        }

        model.addAttribute("types",list);
        model.addAttribute("page",page1);
        model.addAttribute("activeId",id);
        return "types";
    }

}
